<?php
class Admin_ArticleController extends Zend_Controller_Action {
	public function init() {
		$layoutPath = APPLICATION_PATH . '/templates/admin';
		$options = array (
				'layout' => 'index',
				'layoutPath' => $layoutPath 
		);
		Zend_Layout::startMvc ( $options );
	}
	public function indexAction() {
		$Condition = array ();
		list ( $this->view->Pager, $this->view->Articles ) = Article::getAll ( $Condition );
	}
	public function createAction() {
		$requests = $this->getRequest ()->getParams ();
		if ($this->getRequest ()->isPost ()) {
			$uploadDir = PUBLIC_PATH . "/uploads/articles/";
			try {
				$Article = new Article ();
				$Article->merge ( $requests );
				$Article->created_date = date ( 'Y-m-d H:i:s', time () );
				$User = new Zend_Session_Namespace ( 'Login' );
				$Article->created_by = $User->User->id;
				$Article->name_plain = Lib_General::text2url ( $requests ['title'] ) ;
				if ($_FILES) {
					$image = self::_uploadFiles ( $uploadDir, Lib_General::text2url ( $requests ['title'] ) );
					$Article->thumbnail = Lib_General::text2url ( $requests ['title'] ) . '.jpg';
				}
				$Article->save();
				$this->view->Message = 'Create article successfull';
			} catch ( Exception $e ) {
				self::_deleteFile ( $uploadDir . Lib_General::text2url ( $requests ['title'] ) . '.jpg' );
				print_r ( $e->getMessage () );
				die ();
			}
		} else {
			unset ( $requests ['id'] );
		}
	}
	private static function _uploadFiles($uploadDir = '', $FileName = '') {
		if (! is_dir ( $uploadDir )) {
			@mkdir ( $uploadDir );
			chmod ( $uploadDir, 0777 );
		}
		// upload thumb image
		$image = new Zend_Form_Element_File ( 'image' );
		$image->setDestination ( $uploadDir );
		$image->addValidator ( 'Count', false, array (
				'min' => 1,
				'max' => 1 
		) );
		// $ExtendFile = pathinfo($FileName);
		// $FileName= basename($FileName,'.'.$ExtendFile['extension']);
		$image->addValidator ( 'Extension', false, 'jpg,png,gif' );
		$image->addFilter ( 'Rename', array (
				'target' => $uploadDir . $FileName . '.jpg',
				'overwrite' => true 
		) );
		$image->receive ();
		return $image->getFileName ( null, false );
	}
	public static function _deleteFile($FileName = '') {
		if (file_exists ( $FileName )) {
			unlink ( $FileName );
		}
	}
	public function editAction() {
		$requests = $this->getRequest ()->getParams ();
		
		$Article = Article::getById ( $requests ['id'] );
	
		if ($Article) {
			
			$this->view->Article = $Article;
			if ($this->getRequest ()->isPost ()) {
				try {
					$Article->merge ( $requests );
						
					$Article->modified_date = date ( 'Y-m-d H:i:s', time () );
					$User = new Zend_Session_Namespace ( 'Login' );
					$Article->modified_by = $User->User->id;
					
					$Article->name_plain = Lib_General::text2url ( $requests ['title'] ) ;
					$uploadDir = PUBLIC_PATH . "/uploads/articles/";
					if ($_FILES && $_FILES['image']['name']!='') {
	
						self::_deleteFile ( $uploadDir.$Article->thumbnail);
						$image = self::_uploadFiles ( $uploadDir, Lib_General::text2url ( $requests ['title'] ) );
						$Article->thumbnail = Lib_General::text2url ( $requests ['title'] ) . '.jpg';
					}
					$Article->save ();
					$this->view->Message = 'Update user successfull';
				} catch ( Exception $e ) {
					if ($_FILES) {
						$image = self::_uploadFiles ( $uploadDir, $Categories->title );
						$Article->thumbnail = Lib_General::text2url ( $Categories->title ) . '.jpg';
						$Article->save ();
					}
					print_r ( $e->getMessage () );
					die ();
				}
			}
		} else {
			$this->_redirect ( '/admin/article' );
		}
	}
}

